package main.kotlin.markdown

/**
 * @since 2018.3.30
 * @version 0.1
 * 某个页面上标签转化成markdown文本后的类
 */

import java.nio.charset.StandardCharsets
import java.io.PrintWriter
import java.io.FileOutputStream
import java.io.OutputStreamWriter

class PageText (var title: String = "", var url: String = "") {
    var text: ArrayList<String> = ArrayList()

    fun add(other: PageText) {
        this.text.addAll(other.text)
    }

    fun addText(text: String) {
        this.text.add(text)
    }

    fun addLink(text: String, link: String) {
        this.text.add("[$text]($link)")
    }

    fun addImg(text: String, link: String) {
        this.text.add("![$text]($link)")
    }

    fun addHeading(text: String, level: Int) {
        this.text.add("${"#".repeat(level)} $text")
    }

    fun writeToMd(file: String) {
        val writer = PrintWriter(OutputStreamWriter(
                FileOutputStream(file), StandardCharsets.UTF_8)
        )

        writer.println("# ${this.title}")
        writer.println("###### ${this.url}")
        writer.print(this.text.joinToString("\n\n"))

        writer.close()
    }

    fun writeTo() {
        val writer = PrintWriter(System.out)

        writer.println("# $title")
        writer.print(this.text.joinToString("\n"))

        writer.flush()
        writer.close()
    }
}